
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];

let index = binarySearch(arr, 2, 0, arr.length - 1);
console.log(index);

function binarySearch(arr, num, start, end) {
    if (start > end) {
        return -1;
    }
    let mid = Math.floor((start + end) / 2);
    if (arr[mid] == num) {
        return mid;
    } else if (arr[mid] > num) {
        return binarySearch(arr, num, start, mid - 1);
    } else {
        return binarySearch(arr, num, mid + 1, end);
    }
}
